/**
 * 
 */
package any.desktop.gwt.client.ui;

import any.gwt.client.ui.Span;
import any.gwt.client.util.DispacherContainer;
import any.gwt.client.util.DispatchEvent;
import any.tag.gwt.client.TagClient;
import any.tag.gwt.client.ui.TagBar;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * TODO: doc me
 * 
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 90 $
 * @version
 */
public class SearchResultBox extends Composite {
    private final FlowPanel  actionBar;
    private final JSONObject result;
    private final TagClient  tagClient;
    private final FlowPanel  titleBar;

    public SearchResultBox(final JSONObject result, final TagClient tagClient) {
        this.result = result;
        this.tagClient = tagClient;
        final FlowPanel outer = new FlowPanel();
        actionBar = new FlowPanel();
        titleBar = new FlowPanel();
        actionBar.addStyleName("bar");
        titleBar.addStyleName("bar");
        outer.add(titleBar);
        outer.add(actionBar);
        initWidget(outer);
        setStyleName("resultBox");
    }

    protected void onLoad() {
        final Span title = new Span(result.get("title").isString()
                .stringValue());
        title.setStyleName("title");
        title.addStyleName(result.get("type").isString().stringValue()
                .toLowerCase());
        titleBar.add(title);
        final Span open = new Span("Open");
        open.addClickListener(new ClickListener() {

            public void onClick(final Widget sender) {
                DispacherContainer.getGlobalDispatcher().dispatch(
                        new DispatchEvent(this, result.get("id").isString()
                                .stringValue(), "openItem"));
                /*
                 * final RequestBuilder rb = new RequestBuilder(
                 * RequestBuilder.POST, "/any/desktop/api/item/" +
                 * result.get("id").isString().stringValue() + "?open"); try {
                 * rb.sendRequest(null, new RequestCallback() { public void
                 * onError(final Request request, final Throwable exception) { //
                 * TODO Auto-generated method stub } public void
                 * onResponseReceived(final Request request, final Response
                 * response) { // TODO Auto-generated method stub } }); } catch
                 * (final RequestException e) { // TODO Auto-generated catch
                 * block e.printStackTrace(); }
                 */
            }
        });
        String url = result.get("path").isString().stringValue();
        if (url.length() > 64) {
            final StringBuffer sb = new StringBuffer();
            sb.append(url.substring(0, 32)).append("...").append(
                    url.substring(url.length() - 32));
            url = sb.toString();
        }
        final Span label = new Span(url);
        open.addStyleName("link");
        label.addStyleName("url");
        actionBar.add(label);
        actionBar.add(open);
        final TagBar tagBar = new TagBar(result.get("uri").isString()
                .stringValue());
        actionBar.add(tagBar);
    }

}
